//
//  ChooseGoodTypeViewController.m
//  yigou
//
//  Created by wangdianlei on 2017/2/6.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "ChooseGoodTypeViewController.h"
#import "MMMarketType.h"

@interface ChooseGoodTypeViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *typeArray;

@end

@implementation ChooseGoodTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView *blankView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    self.tableView.tableFooterView = blankView;
    [self loadNetworkData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"选择发布商品分类"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(goPop)];
    self.navigationItem.leftBarButtonItem = backBtn;
}

- (void)goPop {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setCellStyle:(UITableViewCell *)cell {
    cell.textLabel.textColor = [UIColor colorWithHexString:@"3d3d3d"];
    //cell.textLabel.font = [FontTool fontWithSize:14];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.typeArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MMMarketType *one = self.typeArray[indexPath.row];
    UITableViewCell *cell = GET_SYSTEM_CELL(Default, @"setdefault");
    [self setCellStyle:cell];
    cell.textLabel.text = one.type_name;
    cell.accessoryType = UITableViewCellAccessoryNone;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:one.img_url]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MMMarketType *one = self.typeArray[indexPath.row];
    [PublishData sharedPublishData].product_type_id = one.theid;
    [PublishData sharedPublishData].product_type_name = one.type_name;
    [[NSNotificationCenter defaultCenter] postNotificationName:MSGSELECTTYPE object:nil];
    [self goPop];
}

- (void)loadNetworkData {
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = [NSString stringWithFormat:@"%@/product/showProductType", APIYIGOU];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        [JSONModel addDatasFromDict:dict withModel:@"MMMarketType" forKey:@"data" target:self.typeArray isAppend:NO];
        [self.tableView reloadData];
    } error:^(YTKBaseRequest *request) {
    } failure:^(YTKBaseRequest *request) {
    }];
}

ARRAY_INIT(typeArray)

@end
